home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Macintosh Sample Code / SC.016.OffSample / OffSample.p < prev    next >
Encoding:
Text File  |  1992-06-11  |  44.0 KB  |  1,529 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    Offscreen Buffer Sample Application
  6. #
  7. #    OffSample
  8. #
  9. #    OffSample.p        -    Pascal Source
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    
  15. #                1.00                04/89
  16. #                1.01                06/92
  17. #
  18. #    Components:
  19. #                OffSample.p            April 1, 1989
  20. #                OffSample.r            April 1, 1989
  21. #                OffSample.h            April 1, 1989
  22. #                OffSample.rsrc        April 1, 1989
  23. #                POffSample.make        April 1, 1989
  24. #
  25. #    Requirements:
  26. #                Offscreen.p            April 1, 1989
  27. #                Offscreen.inc1.p    April 1, 1989
  28. #                UFailure.p            November 1, 1988
  29. #                UFailure.inc1.p        November 1, 1988
  30. #                UFailure.a            November 1, 1988
  31. #
  32. #    OffSample demonstrates the usage of the Offscreen
  33. #    unit. It shows how to use offscreen pixmaps and
  34. #    bitmaps to produce flicker-free updating with a
  35. #    minimum of re-structuring of code. OffSample attempts
  36. #    to reduce the amount of 'knowledge' that it has of
  37. #    the offscreen structure so as to minimize its
  38. #    dependence on that unit.
  39. #
  40. #    OffSample emphasizes using the Offscreen unit; it
  41. #    is not intended to be viewed as a complete application
  42. #    from which to base some larger effort. Instead, its
  43. #    method of using offscreen bitmaps and pixmaps should
  44. #    be studied and adapted to other applications that
  45. #    desire features such as flicker-free updating.
  46. #
  47. ------------------------------------------------------------------------------}
  48.  
  49.  
  50. PROGRAM OffSample;
  51.  
  52. USES
  53.     MemTypes, QuickDraw, Palettes, OSIntf, ToolIntf,
  54.     PackIntf, Picker, UFailure, Offscreen, Traps;
  55.  
  56. CONST
  57.  
  58.     kSysEnvironsVersion        = 1;
  59.  
  60.     kOSEvent                = app4Evt;        {event used by MultiFinder}
  61.     kSuspendResumeMessage    = 1;            {high byte of suspend/resume event message}
  62.     kResumeMask                = 1;            {bit of msg field for resume vs. suspend}
  63.  
  64.     kMinHeap                = 66 * 1024;
  65.     
  66.     kMinSpace                = 49 * 1024;
  67.     
  68.     kExtremeNeg                = -32768;
  69.     kExtremePos                = 32767 - 1;    {required for old region bug}
  70.     
  71.     sErrStrings                = 128;            {error string STR#}
  72.     eStandardErr            = 1;
  73.     eWrongMachine            = 2;
  74.     eSmallSize                = 3;
  75.     eNoMemory                = 4;
  76.     
  77.     kNoBackBuff                = 128;
  78.     kNoEditBuff                = 129;
  79.     kTitle                    = 130;
  80.     kColorPrompt            = 131;
  81.     kNoWantBack                = 132;
  82.     kNoWantEdit                = 133;
  83.     
  84.     kCMoof                    = 128;
  85.     kGigantor                = 128;
  86.     k1bitGigantor            = 129;
  87.     
  88.     rMenuBar                = 128;            {application's menu bar}
  89.     rAboutAlert                = 128;            {about alert}
  90.     rUserAlert                = 129;            {error user alert}
  91.     rWindow                    = 128;            {application's window}
  92.  
  93.     mApple                    = 128;            {Apple menu}
  94.     iAbout                    = 1;
  95.  
  96.     mFile                    = 129;            {File menu}
  97.     iNew                    = 1;
  98.     iClose                    = 4;
  99.     iQuit                    = 12;
  100.  
  101.     mEdit                    = 130;            {Edit menu}
  102.     iUndo                    = 1;
  103.     iCut                    = 3;
  104.     iCopy                    = 4;
  105.     iPaste                    = 5;
  106.     iClear                    = 6;
  107.     
  108.     mShape                    = 131;            {Shape menu}
  109.     
  110.     mSpecial                = 132;            {Special menu}
  111.     iUseBack                = 1;
  112.     iUseEdit                = 2;
  113.     iPickColor                = 4;
  114.  
  115.     kDITop                    = $0050;
  116.     kDILeft                    = $0070;
  117.     
  118.     kNotDrawn                = -1;
  119.     kLastOne                = -2;
  120.     
  121.     kCursorDepth            = 2;
  122.     kMemoryPolite            = TRUE;
  123.     
  124.     kFramePenH                = 2;
  125.     kFramePenV                = 2;
  126.     
  127.     
  128. TYPE
  129.  
  130.     Shapes            = (kOval, kRegion, kRRect, kPoly, kRect, kICON, kPICT);
  131.  
  132.     ShapeRecord        = RECORD
  133.         next            : Shapes;        {when is it drawn?}
  134.         extent            : Rect;            {where is it?}
  135.     END;
  136.                     
  137.     ShapeArray        = ARRAY [Shapes] OF ShapeRecord;
  138.     
  139.     {An OffscreenRecord contains the WindowRecord for one of our sample windows,
  140.      as well as an offscreen handle for the background and an offscreen handle
  141.      for the background plus the shape being created. It also has an array of
  142.      shapes for this window, a pointer to the first shape, a pointer to the
  143.      shape being edited, and a record of the last state of the buffers. For a
  144.      similar example of extending a toolbox data structure, see how the Window
  145.      Manager and Dialog Manager add fields to the GrafPort and WindowRecord,
  146.      respectively.}
  147.      
  148.     OffscreenRecord    = RECORD
  149.         fWindow        : WindowRecord;        {window data structure for toolbox use}
  150.         fBackHandle    : Handle;            {offscreen pixmap that holds background}
  151.         fEditHandle    : Handle;            {pixmap for background and shape being created}
  152.         fShapes        : ShapeArray;        {the shapes for this window}
  153.         fFirst        : Shapes;            {who is first?}
  154.         fEdit        : Shapes;            {who is being edited?}
  155.         fHasBack    : BOOLEAN;            {did it have a background buffer last time?}
  156.         fHasEdit    : BOOLEAN;            {did it have a edit buffer last time?}
  157.     END;
  158.     OffscreenPeek    = ^OffscreenRecord;
  159.  
  160.  
  161. VAR
  162.     {The "g" prefix is used to emphasize that a variable is global.}
  163.  
  164.     gMac                : SysEnvRec;    {set up by Initialize}
  165.     gHasWaitNextEvent    : BOOLEAN;        {set up by Initialize}
  166.     gInBackground        : BOOLEAN;        {maintained by Initialize and DoEvent}
  167.     
  168.     gShape                : Shapes;        {current shape}
  169.     gUseBack            : BOOLEAN;        {create background offscreen flag}
  170.     gUseEdit            : BOOLEAN;        {create edit offscreen flag}
  171.     gCursor                : CCrsrHandle;    {there can be ONLY one}
  172.     gOughHandle            : Handle;        {offscreen handle for color cursor}
  173.     gPICT                : PicHandle;    {Gigantor}
  174.     gcicn                : CIconHandle;    {Moof!™}
  175.     g1BitHandle            : Handle;        {for the color cursor mask}
  176.  
  177.  
  178. {$S Initialize}
  179. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  180.  
  181. {Check to see if a given trap is implemented. This is only used by the
  182.  Initialize routine in this program, so we put it in the Initialize segment.
  183.  The recommended approach to see if a trap is implemented is to see if
  184.  the address of the trap routine is the same as the address of the
  185.  Unimplemented trap. Needs to be called after call to SysEnvirons so that
  186.  it can check if a ToolTrap is out of range of a pre-MacII ROM.}
  187.  
  188. BEGIN
  189.     IF (tType = ToolTrap) &
  190.         (gMac.machineType > envMachUnknown) &
  191.         (gMac.machineType < envMacII) THEN BEGIN        {it's a 512KE, Plus, or SE}
  192.         tNumber := BAND(tNumber, $03FF);
  193.         IF tNumber > $01FF THEN                            {which means the tool traps}
  194.             tNumber := _Unimplemented;                    {only go to $01FF}
  195.     END;
  196.     TrapAvailable := NGetTrapAddress(tNumber, tType) <>
  197.                         GetTrapAddress(_Unimplemented);
  198. END; {TrapAvailable}
  199.  
  200.  
  201. {$S Main}
  202. PROCEDURE GetGlobalRect (window: WindowPtr; VAR globalRect: Rect);
  203.  
  204. {Return the portRect of window in global coordinates.}
  205.  
  206. VAR
  207.     savePort    : GrafPtr;
  208.     
  209. BEGIN
  210.     GetPort(savePort);
  211.     SetPort(window);                    {so that the correct }
  212.     globalRect := window^.portRect;        { coordinate system is used}
  213.     WITH globalRect DO BEGIN
  214.         LocalToGlobal(topLeft);
  215.         LocalToGlobal(botRight);
  216.     END;
  217.     SetPort(savePort);
  218. END; {GetGlobalRect}
  219.  
  220.  
  221. {$S Main}
  222. FUNCTION IsDAWindow (window: WindowPtr): BOOLEAN;
  223.  
  224. {Check if a window belongs to a desk accessory.}
  225.  
  226. BEGIN
  227.     IF window = NIL THEN
  228.         IsDAWindow := FALSE
  229.     ELSE    {DA windows have negative windowKinds}
  230.         IsDAWindow := WindowPeek(window)^.windowKind < 0;
  231. END; {IsDAWindow}
  232.  
  233.  
  234. {$S Main}
  235. FUNCTION IsAppWindow (window: WindowPtr): BOOLEAN;
  236.  
  237. {Check to see if a window belongs to the application. If the window pointer
  238.  passed was NIL, then it could not be an application window. WindowKinds
  239.  that are negative belong to the system and windowKinds less than userKind
  240.  are reserved by Apple except for windowKinds equal to dialogKind, which
  241.  mean it is a dialog.}
  242.  
  243. BEGIN
  244.     IF window = NIL THEN
  245.         IsAppWindow := FALSE
  246.     ELSE    {application windows have windowKinds >= userKind (8)}
  247.         WITH WindowPeek(window)^ DO
  248.             IsAppWindow := (windowKind = userKind);
  249. END; {IsAppWindow}
  250.  
  251.  
  252. {$S Main}
  253. PROCEDURE FailNILMsg(p: UNIV Ptr; message: INTEGER);
  254.  
  255. {Check for NIL p and fail if so.}
  256.  
  257. BEGIN
  258.     IF p = NIL THEN
  259.         Failure(memFullErr, message);
  260. END; {FailNILMsg}
  261.  
  262.  
  263. {$S Main}
  264. PROCEDURE AlertUser(error: INTEGER; message: LongInt);
  265.  
  266. {Display an alert to inform the user of an error. Message acts as an 
  267.  index into a STR# resource of error messages. If no message is given,
  268.  i.e. = 0, then use a standard message. If error is not noErr then
  269.  display it as well.}
  270.  
  271. VAR
  272.     msg1, msg2    : Str255;
  273.     itemHit        : INTEGER;
  274. BEGIN
  275.     IF message = 0 THEN message := eStandardErr;
  276.     GetIndString(msg1, sErrStrings, message);
  277.     IF error = noErr THEN
  278.         msg2 := ''
  279.     ELSE
  280.         NumToString(error, msg2);
  281.     ParamText(msg1, msg2, '', '');
  282.     itemHit := Alert(rUserAlert, NIL);
  283. END; {AlertUser}
  284.  
  285.  
  286. {$S Main}
  287. FUNCTION DoCloseWindow(window: WindowPtr) : BOOLEAN;
  288.  
  289. {Close a window.}
  290.  
  291. {At this point, if there was a document associated with a
  292.  window, you could do any document saving processing if it is 'dirty'.
  293.  DoCloseWindow would return TRUE if the window actually closes, i.e.,
  294.  the user does not cancel from a save dialog. This result is handy when
  295.  the user quits an application, but then cancels a save of a document
  296.  associated with a window. We also added code to close the application
  297.  window since otherwise, the termination routines would never stop looping,
  298.  waiting for FrontWindow to return NIL.}
  299.  
  300. VAR
  301.     pal    : PaletteHandle;
  302.  
  303. BEGIN
  304.     DoCloseWindow := TRUE;
  305.     IF IsDAWindow(window) THEN
  306.         CloseDeskAcc(WindowPeek(window)^.windowKind);
  307.     IF IsAppWindow(window) THEN BEGIN
  308.         WITH OffscreenPeek(window)^ DO BEGIN
  309.             DisposeOffscreen(fBackHandle);
  310.             DisposeOffscreen(fEditHandle);
  311.         END;
  312.         IF gMac.hasColorQD THEN BEGIN
  313.             pal := GetPalette(window);            {We must handle this ourselves,}
  314.             DisposePalette(pal);                {since we may have done a GetPalette.}
  315.         END;
  316.         CloseWindow(window);                    {Since we provided our own storage.}
  317.         DisposPtr(Ptr(window));
  318.     END;
  319. END; {DoCloseWindow}
  320.  
  321.  
  322. {$S Main}
  323. PROCEDURE EfficientConcat2 (VAR string1, string2: Str255);
  324.  
  325. {Do a more efficient concat than CONCAT since we know
  326.  there are only two strings.}
  327.  
  328. VAR
  329.     len1, len2    : INTEGER;
  330.     
  331. BEGIN
  332.     len1 := LENGTH(string1);
  333.     IF len1 < 255 THEN BEGIN
  334.         len2 := LENGTH(string2);
  335.         IF len1 + len2 > 255 THEN
  336.             len2 := 255 - len1;
  337.         BlockMove(@string2[1], @string1[1 + len1], len2);
  338.         string1[0] := CHR(len1 + len2);
  339.     END;
  340. END; {EfficientConcat2}
  341.  
  342.  
  343. {$S Main}
  344. PROCEDURE AppendTitle (VAR title: Str255; id: INTEGER);
  345.  
  346. {Append the specified string resource data to the provided
  347.  string.}
  348.  
  349.  
  350. VAR
  351.     aString    : StringHandle;
  352.     
  353. BEGIN
  354.     aString := GetString(id);
  355.     IF aString <> NIL THEN BEGIN
  356.         HLock(Handle(aString));                {in case EfficientConcat2 is}
  357.         EfficientConcat2(title, aString^^);
  358.         HUnlock(Handle(aString));            {in a different segment}
  359.     END;
  360. END; {AppendTitle}
  361.  
  362.  
  363. {$S Main}
  364. PROCEDURE CheckTitle (window: WindowPtr; doCheck: BOOLEAN);
  365.  
  366. {Compare the prior state of the offscreen handles for
  367.  window and change its title to reflect the new state.}
  368.  
  369. VAR
  370.     aString                : StringHandle;
  371.     title                : Str255;
  372.     hasBack, hasEdit    : BOOLEAN;
  373.  
  374. BEGIN
  375.     IF IsAppWindow(window) THEN
  376.         WITH OffscreenPeek(window)^ DO BEGIN
  377.             hasBack := (GetMap(fBackHandle) <> NIL);
  378.             hasEdit := (GetMap(fEditHandle) <> NIL);
  379.             IF (NOT doCheck) |                    {set title regardless}
  380.             (fHasBack <> hasBack) |                {or if change}
  381.             (fHasEdit <> hasEdit) THEN BEGIN    {in buffers}
  382.                 fHasBack := hasBack;
  383.                 fHasEdit := hasEdit;
  384.                 title := '';
  385.                 aString := GetString(kTitle);
  386.                 IF aString <> NIL THEN
  387.                     title := aString^^;
  388.                     
  389.                 {If an offscreen handle is NIL, it means
  390.                  that the creation of that offscreen handle
  391.                  was disabled by the user. Once that is
  392.                  done, the buffer will never be created.}
  393.                 
  394.                 IF fBackHandle = NIL THEN
  395.                     AppendTitle(title, kNoWantBack)
  396.                 ELSE IF NOT hasBack THEN
  397.                     AppendTitle(title, kNoBackBuff);
  398.                 IF fEditHandle = NIL THEN
  399.                     AppendTitle(title, kNoWantEdit)
  400.                 ELSE IF NOT hasEdit THEN
  401.                     AppendTitle(title, kNoEditBuff);
  402.                 SetWTitle(window, title);
  403.             END;
  404.         END;
  405. END; {CheckTitle}
  406.  
  407.  
  408. {$S Main}
  409. PROCEDURE DrawShape (shape: Shapes; VAR extent: Rect);
  410.  
  411. {Draw the shape specified in the extent. Extent is a VAR
  412.  parameter because the region and polygon are generated
  413.  from the extent rect and the calculations might result
  414.  in a final shape larger than the original extent.}
  415.  
  416.     PROCEDURE DoRegion;
  417.     
  418.     {Generate a region based on the extent.}
  419.     
  420.     VAR
  421.         r        : Rect;
  422.         rHandle    : RgnHandle;
  423.         pHandle    : PolyHandle;
  424.         
  425.     BEGIN
  426.         r := extent;
  427.         rHandle := NewRgn;
  428.         OpenRgn;
  429.         
  430.         FrameRect(extent);
  431.         WITH r DO BEGIN
  432.             top := top + ((bottom - top) DIV 3);
  433.             bottom := top + ((bottom - top) DIV 2);
  434.         END;
  435.         FrameOval(r);
  436.         pHandle := OpenPoly;
  437.         WITH extent DO BEGIN
  438.             MoveTo(left, top);
  439.             LineTo(right, bottom);
  440.             LineTo(left + (right - left) DIV 2, bottom - (bottom - top) DIV 3);
  441.             LineTo(left, top);
  442.         END;
  443.         ClosePoly;
  444.         FramePoly(pHandle);
  445.         KillPoly(pHandle);
  446.         
  447.         CloseRgn(rHandle);
  448.         extent := rHandle^^.rgnBBox;        {in case bigger than original rect}
  449.         IF gMac.hasColorQD THEN
  450.             PaintRgn(rHandle)
  451.         ELSE
  452.             FillRgn(rHandle, black);
  453.         ForeColor(blackColor);
  454.         FrameRgn(rHandle);
  455.         DisposeRgn(rHandle);
  456.     END; {DoRegion}
  457.     
  458.     PROCEDURE DoPoly;
  459.     
  460.     {Generate a polygon based on the extent.}
  461.     
  462.     VAR
  463.         pHandle    : PolyHandle;
  464.         
  465.     BEGIN
  466.         pHandle := OpenPoly;
  467.         WITH extent DO BEGIN
  468.             MoveTo(left + (right - left) DIV 2, top);
  469.             LineTo(right, bottom);
  470.             LineTo(left, top + (bottom - top) DIV 3);
  471.             LineTo(right, top + (bottom - top) DIV 3);
  472.             LineTo(left, bottom);
  473.             LineTo(left + (right - left) DIV 2, top);
  474.         END;
  475.         ClosePoly;
  476.         extent := pHandle^^.polyBBox;        {in case bigger than original rect}
  477.         IF gMac.hasColorQD THEN
  478.             PaintPoly(pHandle)
  479.         ELSE
  480.             FillPoly(pHandle, ltGray);
  481.         ForeColor(blackColor);
  482.         FramePoly(pHandle);
  483.         KillPoly(pHandle);
  484.     END; {DoPoly}
  485.     
  486. BEGIN
  487.     PenNormal;
  488.     PenSize(kFramePenH, kFramePenV);
  489.     CASE shape OF
  490.         kOval: BEGIN
  491.             IF gMac.hasColorQD THEN
  492.                 PaintOval(extent)
  493.             ELSE
  494.                 FillOval(extent, white);
  495.             ForeColor(blackColor);
  496.             FrameOval(extent);
  497.         END;
  498.         kRegion:
  499.             DoRegion;
  500.         kRRect: BEGIN
  501.             IF gMac.hasColorQD THEN
  502.                 PaintRoundRect(extent, 16, 16)
  503.             ELSE
  504.                 FillRoundRect(extent, 16, 16, gray);
  505.             ForeColor(blackColor);
  506.             FrameRoundRect(extent, 16, 16);
  507.         END;
  508.         kPoly:
  509.             DoPoly;
  510.         kRect: BEGIN
  511.             IF gMac.hasColorQD THEN
  512.                 PaintRect(extent)
  513.             ELSE
  514.                 FillRect(extent, dkGray);
  515.             ForeColor(blackColor);
  516.             FrameRect(extent);
  517.         END;
  518.         kICON:
  519.             IF gMac.hasColorQD THEN
  520.                 PlotCIcon(extent, gcicn)
  521.             ELSE BEGIN 
  522.                 HLock(Handle(gcicn));
  523.                 WITH gcicn^^ DO BEGIN
  524.                 {We cannot call PlotCIcon when Color QD is not
  525.                  present, but we can still use the color icon
  526.                  data.}
  527.                     iconMask.baseAddr := @iconMaskData;
  528.                     iconBMap.baseAddr := Ptr(ORD(@iconMaskData) + 128);
  529.                     CopyMask(iconBMap, iconMask, thePort^.portBits,
  530.                                 iconBMap.bounds, iconMask.bounds, extent);
  531.                 END;
  532.                 HUnlock(Handle(gcicn));
  533.             END;
  534.         kPICT:
  535.             DrawPicture(gPICT, extent);
  536.     END;
  537. END; {DrawShape}
  538.  
  539.  
  540. {$S Main}
  541. FUNCTION GimmeBlackAndWhite(rgb: RGBColor; VAR position: LONGINT): BOOLEAN;
  542.  
  543. {This is a search proc that returns white only if the color is really white;
  544.  otherwise it returns black. It is used to generate the mask for the color
  545.  cursor. It boldly assumes that it is being called for a 1 bit deep map.}
  546.  
  547. BEGIN
  548.     WITH rgb DO
  549.         IF (red = $FFFF) & (green = $FFFF) & (blue = $FFFF) THEN
  550.             position := 0                    {return white if it’s white}
  551.         ELSE
  552.             position := 1;                    {else return black for all other colors}
  553.     GimmeBlackAndWhite := TRUE;
  554. END; {GimmeBlackAndWhite}
  555.  
  556.  
  557. {$S Main}
  558. PROCEDURE SetObjCursor (window: WindowPtr);
  559.  
  560. {Build the color cursor. Note that this routine is only called
  561.  in a Color QD environment, so it doesn't have to make the
  562.  check. Note also that the cursors could have all been 'pre-
  563.  built', thus making things more efficient, but this example
  564.  shows that dynamic cursors can be implemented via pixmaps.}
  565.  
  566. VAR
  567.     colors            : CTabHandle;
  568.     pal                : PaletteHandle;
  569.     rgb                : RGBColor;
  570.     bounder, extent    : Rect;
  571.     buffNotNeeded    : BOOLEAN;
  572.     naughtyBits        : BitMap;
  573.     oneBitPMap        : BitMapPtr;
  574.  
  575. BEGIN
  576.     SetRect(bounder, 0, 0, 16, 16);
  577.     pal := GetPalette(window);
  578.     GetEntryColor(pal, ORD(gShape) + 2, rgb);    {get the color used for the shape}
  579.     
  580.     DisposeOffscreen(gOughHandle);                {get rid of old color table}
  581.     colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  582.     FailNILMsg(colors, eNoMemory);
  583.     colors^^.ctTable[0].rgb := rgb;                {stuff in the color we want}
  584.     
  585.     FailOSErr(NewOffscreen(bounder, kCursorDepth, colors,
  586.                     NOT kMemoryPolite, buffNotNeeded,
  587.                     gOughHandle));
  588.     DisposHandle(Handle(colors));
  589.     
  590.     HLock(Handle(gCursor));
  591.     WITH gCursor^^ DO BEGIN
  592.         crsrMap := PixMapHandle(RecoverHandle(Ptr(GetMap(gOughHandle))));
  593.         crsrData := GetBitsHandle(gOughHandle);
  594.         IF crsrData = NIL THEN BEGIN            {no handle to bits available-punt}
  595.             SetCursor(arrow);
  596.             Exit(SetObjCursor);
  597.         END;
  598.         BeginOffscreenDrawing(gOughHandle, NIL);
  599.         IF NOT (gShape IN [kICON, kPICT]) THEN BEGIN
  600.             SetPt(crsrHotSpot, 0, 0);
  601.             RGBForeColor(rgb);
  602.             extent := bounder;
  603.             InsetRect(extent, 3, 1);            {squeeze it a bit}
  604.             DrawShape(gShape, extent);            {draw the cursor shape}
  605.             PenNormal;
  606.             ForeColor(blackColor);                {draw hot spot}
  607.             MoveTo(0, 0);
  608.             LineTo(0, 1);
  609.         END ELSE BEGIN                            {use a plain cursor for icon/pict}
  610.             SetPt(crsrHotSpot, 2, 2);
  611.             PenNormal;
  612.             ForeColor(blackColor);
  613.             MoveTo(0, 0);
  614.             LineTo(4, 4);
  615.             MoveTo(4, 0);
  616.             LineTo(0, 4);
  617.         END;
  618.         EndOffscreenDrawing(gOughHandle);
  619.         WITH naughtyBits DO BEGIN                {build 1-bit image and mask}
  620.             bounds := bounder;
  621.             baseAddr := @crsr1Data;
  622.             rowBytes := 2;
  623.             CopyBits(BitMapPtr(crsrMap^)^, naughtyBits,
  624.                         bounder, bounder, srcCopy, NIL);
  625.                         
  626.             oneBitPMap :=  GetMap(g1BitHandle);
  627.             oneBitPMap^.baseAddr := @crsrMask;
  628.             AddSearch(@GimmeBlackAndWhite);
  629.             CopyBits(BitMapPtr(crsrMap^)^, oneBitPMap^,
  630.                         bounder, bounder, srcCopy, NIL);
  631.             DelSearch(@GimmeBlackAndWhite);
  632.         END;
  633.         crsrXValid := 0;
  634.         crsrID := GetCTSeed;
  635.     END;
  636.     HUnlock(Handle(gCursor));
  637. END; {SetObjCursor}
  638.  
  639.  
  640. {$S Main}
  641. FUNCTION GetInvalExtent (window: WindowPtr; shape: Shapes) : Rect;
  642.  
  643. {Return the shape's extent, adjusted for the pensize of the frame.}
  644.  
  645. VAR
  646.     r    : Rect;
  647.     
  648. BEGIN
  649.     r := OffscreenPeek(window)^.fShapes[shape].extent;
  650.     WITH r DO BEGIN
  651.         right := right + kFramePenH;
  652.         bottom := bottom + kFramePenV;
  653.     END;
  654.     GetInvalExtent := r;
  655. END; {GetInvalExtent}
  656.  
  657.  
  658. {$S Main}
  659. PROCEDURE ChangeColor (window: WindowPtr);
  660.  
  661. {Display the Color Picker dialog. Note that this is
  662.  only called in Color QD environments.}
  663.  
  664. VAR
  665.     pal                    : PaletteHandle;
  666.     inColor, outColor    : RGBColor;
  667.     where                : Point;
  668.     r                    : Rect;
  669.     aString                : StringHandle;
  670.     prompt                : Str255;
  671.  
  672. BEGIN
  673.     pal := GetPalette(window);
  674.     WITH OffscreenPeek(window)^ DO BEGIN
  675.         GetEntryColor(pal, ORD(gShape) + 2, inColor);
  676.         SetPt(where, kDILeft, kDITop);
  677.         aString := GetString(kColorPrompt);
  678.         IF aString <> NIL THEN
  679.             prompt := aString^^
  680.         ELSE
  681.             prompt := '';
  682.         IF GetColor(where, prompt, inColor, outColor) THEN BEGIN
  683.             SetEntryColor(pal, ORD(gShape) + 2, outColor);
  684.             ActivatePalette(window);
  685.             SetObjCursor(window);
  686.             IF NOT (fShapes[gShape].next = Shapes(kNotDrawn)) THEN BEGIN
  687.                 r := GetInvalExtent(window, gShape);
  688.                 SetPort(window);
  689.                 InvalRect(r);
  690.             END;
  691.         END;
  692.     END;
  693. END; {ChangeColor}
  694.  
  695.  
  696. {$S Main}
  697. PROCEDURE DoNewWindow;
  698.  
  699. {We will allocate our own window storage instead of letting the Window
  700.  Manager do it for two reasons. One, GetNewWindow locks the 'WIND' resource
  701.  handle before calling NewWindow and this can lead to heap fragmentation
  702.  in low memory situations. Two, it takes just as much time for NewWindow
  703.  to get the memory as it does for us to get it. Three, there are THREE
  704.  reasons we will allocate our own window storage instead of letting the
  705.  Window Manager do it. One, GetNewWindow locks etc. etc. Two, it takes
  706.  just as much time, etc. etc. And three, this way we can allocate larger records
  707.  where the extra space can be used to connect other, related data structures.
  708.  Four, there is no fourth reason.}
  709.  
  710. VAR
  711.     p                : Ptr;
  712.     window            : WindowPtr;
  713.     noBuffsPlease    : BOOLEAN;
  714.     title            : Str255;
  715.     shape            : Shapes;
  716.     emptyRect        : Rect;
  717.  
  718. BEGIN
  719.     p := NewPtr(SIZEOF(OffscreenRecord));
  720.     FailNILMsg(p, eNoMemory);
  721.     window := NIL;
  722.     IF gMac.hasColorQD THEN
  723.         window := GetNewCWindow(rWindow, p, WindowPtr(-1))
  724.     ELSE
  725.         window := GetNewWindow(rWindow, p, WindowPtr(-1));
  726.     FailNILMsg(window, eNoMemory);
  727.     
  728.     WITH OffscreenPeek(window)^ DO BEGIN
  729.         fBackHandle := NIL;
  730.         fEditHandle := NIL;
  731.         fHasBack := FALSE;
  732.         fHasEdit := FALSE;
  733.         IF gUseBack THEN
  734.             IF NewOffscreenForWindow(window, noBuffsPlease, fBackHandle) = noErr THEN;
  735.         IF gUseEdit THEN
  736.             IF NewOffscreenForWindow(window, noBuffsPlease, fEditHandle) = noErr THEN;
  737.         SetRect(emptyRect, 0, 0, 0, 0);
  738.         FOR shape := kOval TO kPICT DO BEGIN
  739.             fShapes[shape].next := Shapes(kNotDrawn);
  740.             fShapes[shape].extent := emptyRect;
  741.         END;
  742.         fFirst := Shapes(kNotDrawn);
  743.         fEdit := Shapes(kNotDrawn);
  744.     END;
  745.     
  746.     CheckTitle(window, FALSE);
  747.     IF gMac.hasColorQD THEN
  748.         SetObjCursor(window);
  749. END; {DoNewWindow}
  750.  
  751.  
  752. {$S Initialize}
  753. PROCEDURE Initialize;
  754.  
  755. {Set up the whole world, including global variables, Toolbox managers,
  756.  and menus. We also create one application window at this time.
  757.  Since window storage is non-relocateable, how and when to allocate space
  758.  for windows is very important so that heap fragmentation does not occur.
  759.  Window storage can differ widely amongst applications depending on how many
  760.  windows are created and disposed. If a failure occurs here, we will consider
  761.  that the application is in such bad shape that we should just exit. Your error
  762.  handling may differ, but the checks should still be made.}
  763.  
  764. TYPE
  765.     crsColors = ARRAY[0..2] OF ColorSpec;
  766.  
  767. VAR
  768.     menuBar            : Handle;
  769.     ignoreError        : OSErr;
  770.     total, contig    : LongInt;
  771.     ignoreResult    : BOOLEAN;
  772.     event            : EventRecord;
  773.     count            : INTEGER;
  774.     fi                : FailInfo;
  775.     colors            : CTabHandle;
  776.     bounder            : Rect;
  777.     buffNotNeeded    : BOOLEAN;
  778.  
  779.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  780.     BEGIN
  781.         IF error > 0 THEN
  782.             AlertUser(0, error)
  783.         ELSE
  784.             AlertUser(error, message);
  785.         ExitToShell;
  786.     END; {HandleErr}
  787.  
  788. BEGIN
  789.     gInBackground := FALSE;
  790.  
  791.     InitGraf(@thePort);
  792.     InitFonts;
  793.     InitWindows;
  794.     InitMenus;
  795.     TEInit;
  796.     InitDialogs(NIL);
  797.     InitCursor;
  798.     
  799.     InitOffscreen;
  800.  
  801.     {Call OpenDriver('.MPP', refnum) at this point to initialize AppleTalk,
  802.      if you are using it.}
  803.      
  804.     {NOTE -- It is no longer necessary, and actually unhealthy, to check
  805.      PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  806.      of checking for port availability themselves.}
  807.     
  808.     {This next bit of code is necessary to allow the default button of our
  809.      alert to be outlined.}
  810.      
  811.     FOR count := 1 TO 3 DO
  812.         ignoreResult := EventAvail(everyEvent, event);
  813.  
  814.     CatchFailures(fi, HandleErr);
  815.  
  816.     {Ignore the error returned from SysEnvirons; even if an error occurred,
  817.      the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  818.      call to SysEnvirons by calling it after initializing AppleTalk.}
  819.      
  820.     ignoreError := SysEnvirons(kSysEnvironsVersion, gMac);
  821.     
  822.     {Make sure that the machine has at least 128K ROMs. If it doesn't, exit.}
  823.     
  824.     IF gMac.machineType < 0 THEN
  825.         Failure(0, eWrongMachine);
  826.     
  827.     {Move TrapAvailable call to after SysEnvirons so that we can tell
  828.      in TrapAvailable if a tool trap value is out of range.}
  829.      
  830.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  831.  
  832.     {First check the size of the application heap against a value
  833.      that you have determined is the smallest heap the application can reasonably
  834.      work in. This number should be derived by examining the size of the heap that
  835.      is actually provided by MultiFinder when the minimum size requested is used.
  836.      The derivation of the minimum size requested from MultiFinder is described
  837.      in Sample.h. The check should be made because the preferred size can end up
  838.      being set smaller than the minimum size by the user. This extra check acts to
  839.      insure that your application is starting from a solid memory foundation.}
  840.      
  841.     IF ORD(GetApplLimit) - ORD(ApplicZone) < kMinHeap THEN
  842.         Failure(0, eSmallSize);
  843.     
  844.     {Next, make sure that enough memory is free for your application to run. It
  845.      is possible for a situation to arise where the heap may have been of required
  846.      size, but a large scrap was loaded which left too little memory. To check for
  847.      this, call PurgeSpace and compare the result with a value that you have determined
  848.      is the minimum amount of free memory your application needs at initialization.
  849.      This number can be derived several different ways. One way that is fairly
  850.      straightforward is to run the application in the minimum size configuration
  851.      as described previously. Call PurgeSpace at initialization and examine the value
  852.      returned. However, you should make sure that this result is not being modified
  853.      by the scrap's presence. You can do that by calling ZeroScrap before calling
  854.      PurgeSpace. Make sure to remove that call before shipping, though.}
  855.      
  856.     PurgeSpace(total, contig);
  857.     IF total < kMinSpace THEN
  858.         Failure(0, eNoMemory);
  859.  
  860.     {The extra benefit to waitng until after the Toolbox Managers have been initialized
  861.      before checking memory is that we can now give the user an alert to tell him what
  862.      happened. Although it is possible that the memory situation could be worsened by
  863.      displaying an alert, MultiFinder would gracefully exit the application with
  864.      an informative alert if memory became critical. Here we are acting more
  865.      in a preventative manner to avoid future disaster from low-memory problems.}
  866.  
  867.     menuBar := GetNewMBar(rMenuBar);        {read menus into menu bar}
  868.     FailNILMsg(menuBar, eNoMemory);
  869.     SetMenuBar(menuBar);                    {install menus}
  870.     DisposHandle(menuBar);
  871.     AddResMenu(GetMHandle(mApple), 'DRVR');    {add DA names to Apple menu}
  872.     DrawMenuBar;
  873.     gShape := kOval;
  874.     gUseBack := TRUE;
  875.     gUseEdit := TRUE;
  876.     gOughHandle := NIL;
  877.     
  878.     {Get the 'Moof' icon. If the environment supports Color QD,
  879.      we'll get the color icon. If Color QD is not supported,
  880.      we'll still get the color icon, but use it differently.}
  881.      
  882.     IF gMac.hasColorQD THEN BEGIN
  883.         gcicn := GetCIcon(kCMoof);
  884.         FailNILMsg(gcicn, eNoMemory);
  885.     END ELSE BEGIN
  886.         gcicn := CIconHandle(GetResource('cicn', kCMoof));
  887.         FailNILMsg(gcicn, eNoMemory);
  888.     END;
  889.     
  890.     {If Color QD is supported, we'll get an 8-bit PICT of
  891.      Gigantor. If it isn't supported, we'll get a PICT
  892.      that looks better in non-color ports/}
  893.      
  894.     IF gMac.hasColorQD THEN
  895.         gPICT := GetPicture(kGigantor)
  896.     ELSE
  897.         gPICT := GetPicture(k1bitGigantor);
  898.     FailNILMsg(gPICT, eNoMemory);
  899.     
  900.     {If Color QD is supported, we'll set up a color cursor
  901.      that will be modified later. Otherwise, nothing
  902.      happens. We'll also set up a 1-bit offscreen to
  903.      make a cursor mask.}
  904.      
  905.     IF gMac.hasColorQD THEN BEGIN
  906.         gCursor := CCrsrHandle(NewHandleClear(SIZEOF(CCrsr)));
  907.         FailNILMsg(gCursor, eNoMemory);
  908.         MoveHHi(Handle(gCursor));
  909.         HLock(Handle(gCursor));
  910.         WITH gCursor^^ DO BEGIN
  911.             crsrType := $8001;
  912.             crsrXData := NewHandle(0);
  913.         END;
  914.         HUnlock(Handle(gCursor));
  915.         colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  916.         FailNILMsg(colors, eNoMemory);
  917.         SetRect(bounder, 0, 0, 16, 16);
  918.         
  919.         {For this one bit deep offscreen guy (used to make cursor masks)
  920.          we pass in a zeroed but otherwise unitialized color table. Since
  921.          the map’s ctable will only have B&W anyway, it doesn’t matter.}
  922.          
  923.         FailOSErr(NewOffscreen(bounder, 1, colors,
  924.                         NOT kMemoryPolite, buffNotNeeded,
  925.                         g1BitHandle));
  926.         DisposHandle(Handle(colors));
  927.     END;
  928.  
  929.     DoNewWindow;                            {create a new window right away}
  930. END; {Initialize}
  931.  
  932.  
  933. {$S Main}
  934. PROCEDURE Terminate;
  935.  
  936. {Clean up the application and exits. We close all of the windows so that
  937.  they can update their documents, if any.
  938.  If we find out that a cancel has occurred, we won't exit to the
  939.  shell, but will return instead.}
  940.  
  941. VAR
  942.     aWindow    : WindowPtr;
  943.     closed    : BOOLEAN;
  944.  
  945. BEGIN
  946.     closed := TRUE;
  947.     REPEAT
  948.         aWindow := FrontWindow;                    {get the current front window}
  949.         IF aWindow <> NIL THEN
  950.             closed := DoCloseWindow(aWindow);    {close this window}
  951.     UNTIL (NOT closed) | (aWindow = NIL);        {do all windows}
  952.     IF closed THEN
  953.         ExitToShell;                            {exit if no cancellation}
  954. END; {Terminate}
  955.  
  956.  
  957. {$S Main}
  958. PROCEDURE AdjustMenus;
  959.  
  960. {Enable and disable menus based on the current state.
  961.  The user can only select enabled menu items. We set up all the menu items
  962.  before calling MenuSelect or MenuKey, since these are the only times that
  963.  a menu item can be selected. Note that MenuSelect is also the only time
  964.  the user will see menu items. This approach to deciding what enable/
  965.  disable state a menu item has the advantage of concentrating all the decision-
  966.  making in one routine, as opposed to being spread throughout the application.
  967.  Other application designs may take a different approach that may or may not be
  968.  just as valid.}
  969.  
  970. VAR
  971.     window            : WindowPtr;
  972.     menu            : MenuHandle;
  973.  
  974. BEGIN
  975.     window := FrontWindow;
  976.  
  977.     menu := GetMHandle(mFile);
  978.     IF IsDAWindow(window) |
  979.         IsAppWindow(window) THEN            {we can allow DAs to be closed from the menu}
  980.         EnableItem(menu, iClose)
  981.     ELSE
  982.         DisableItem(menu, iClose);
  983.  
  984.     menu := GetMHandle(mEdit);
  985.     IF IsDAWindow(window) THEN BEGIN        {a desk accessory might need the edit menu}
  986.         EnableItem(menu, iUndo);
  987.         EnableItem(menu, iCut);
  988.         EnableItem(menu, iCopy);
  989.         EnableItem(menu, iPaste);
  990.         EnableItem(menu, iClear);
  991.     END ELSE BEGIN                            {but we know we do not}
  992.         DisableItem(menu, iUndo);
  993.         DisableItem(menu, iCut);
  994.         DisableItem(menu, iCopy);
  995.         DisableItem(menu, iClear);
  996.         DisableItem(menu, iPaste);
  997.     END;
  998.  
  999.     menu := GetMHandle(mSpecial);
  1000.     IF gMac.hasColorQD & IsAppWindow(window) THEN
  1001.         EnableItem(menu, iPickColor)        {color can change only if we are top}
  1002.     ELSE
  1003.         DisableItem(menu, iPickColor);
  1004. END; {AdjustMenus}
  1005.  
  1006.  
  1007. {$S Main}
  1008. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  1009.  
  1010. {This is called when an item is chosen from the menu bar (after calling
  1011.  MenuSelect or MenuKey). It performs the right operation for each command.
  1012.  It is good to have both the result of MenuSelect and MenuKey go to
  1013.  one routine like this to keep everything organized.}
  1014.  
  1015. VAR
  1016.     menuID            : INTEGER;        {the resource ID of the selected menu}
  1017.     menuItem        : INTEGER;        {the item number of the selected menu}
  1018.     int                : INTEGER;
  1019.     str                : Str255;
  1020.     ignore            : BOOLEAN;
  1021.  
  1022. BEGIN
  1023.     menuID := HiWrd(menuResult);    {use built-ins (for efficiency)...}
  1024.     menuItem := LoWrd(menuResult);    {to get menu item number and menu number}
  1025.     CASE menuID OF
  1026.         mApple:
  1027.             CASE menuItem OF
  1028.                 iAbout:                {bring up alert for About}
  1029.                     int := Alert(rAboutAlert, NIL);
  1030.                 OTHERWISE BEGIN        {all non-About items in this menu are DAs}
  1031.                     GetItem(GetMHandle(mApple), menuItem, str);
  1032.                     int := OpenDeskAcc(str);
  1033.                 END;
  1034.             END;
  1035.         mFile:
  1036.             CASE menuItem OF
  1037.                 iNew:
  1038.                     DoNewWindow;
  1039.                 iClose:
  1040.                     ignore := DoCloseWindow(FrontWindow); {we don't care if cancelled}
  1041.                 iQuit:
  1042.                     Terminate;
  1043.             END;
  1044.         mEdit:                        {call SystemEdit for DA editing & MultiFinder}
  1045.             ignore := SystemEdit(menuItem-1);    {since we don't do any editing}
  1046.         mShape: 
  1047.             IF gShape <> Shapes(menuItem - 1) THEN BEGIN
  1048.                 CheckItem(GetMHandle(mShape), ORD(gShape) + 1, FALSE);
  1049.                 gShape := Shapes(menuItem - 1);        {the shape is the item}
  1050.                 CheckItem(GetMHandle(mShape), ORD(gShape) + 1, TRUE);
  1051.                 IF gMac.hasColorQD & IsAppWindow(FrontWindow) THEN
  1052.                     SetObjCursor(FrontWindow);
  1053.             END;
  1054.         mSpecial:
  1055.             CASE menuItem OF
  1056.                 iUseBack: BEGIN
  1057.                     gUseBack := NOT gUseBack;
  1058.                     CheckItem(GetMHandle(mSpecial), iUseBack, gUseBack);
  1059.                 END;
  1060.                 iUseEdit: BEGIN
  1061.                     gUseEdit := NOT gUseEdit;
  1062.                     CheckItem(GetMHandle(mSpecial), iUseEdit, gUseEdit);
  1063.                 END;
  1064.                 iPickColor:
  1065.                     ChangeColor(FrontWindow);
  1066.             END;
  1067.     END;
  1068.     HiliteMenu(0);                    {unhighlight what MenuSelect (or MenuKey) hilited}
  1069. END; {DoMenuCommand}
  1070.  
  1071.  
  1072. {$S Main}
  1073. PROCEDURE GoThroughShapes (PROCEDURE WhatToDo(shape: Shapes); window: WindowPtr);
  1074.  
  1075. {Go through the list of shapes for window and
  1076.  call WhatToDo, passing the shape we are on
  1077.  each time.}
  1078.  
  1079. VAR
  1080.     theShape    : Shapes;
  1081.     
  1082. BEGIN
  1083.     WITH OffscreenPeek(window)^ DO
  1084.         IF ORD(fFirst) <> kNotDrawn THEN BEGIN
  1085.             theShape := fFirst;
  1086.             REPEAT
  1087.                 WhatToDo(theShape);
  1088.                 theShape := fShapes[theShape].next;
  1089.             UNTIL ORD(theShape) = kLastOne;
  1090.         END;
  1091. END; {GoThroughShapes}
  1092.  
  1093.  
  1094. {$S Main}
  1095. PROCEDURE DrawAllShapes (window: WindowPtr; doEdit: BOOLEAN);
  1096.  
  1097. {Draw either the currently edited shape or all the shapes
  1098.  in the window's list. Called by DrawWindow.}
  1099.  
  1100. VAR
  1101.     area    : Rect;
  1102.     
  1103.     PROCEDURE AndDrawThem (shape: Shapes);
  1104.     
  1105.     VAR
  1106.         r    : Rect;
  1107.         
  1108.     BEGIN
  1109.         WITH OffscreenPeek(window)^ DO
  1110.             IF shape <> fEdit THEN BEGIN
  1111.                 IF SectRect(OffscreenPeek(window)^.fShapes[shape].extent, area, r)
  1112.                     THEN BEGIN
  1113.                     IF gMac.hasColorQD THEN
  1114.                         PmForeColor(ORD(shape) + 2);
  1115.                     DrawShape(shape, OffscreenPeek(window)^.fShapes[shape].extent);
  1116.                 END;
  1117.             END;
  1118.     END;
  1119.  
  1120. BEGIN
  1121.     SetPort(window);
  1122.     IF doEdit THEN BEGIN
  1123.         WITH OffscreenPeek(window)^ DO                {draw edit shape}
  1124.             IF ORD(fEdit) <> kNotDrawn THEN BEGIN
  1125.                 IF gMac.hasColorQD THEN
  1126.                     PmForeColor(ORD(fEdit) + 2);
  1127.                 DrawShape(fEdit, OffscreenPeek(window)^.fShapes[fEdit].extent);
  1128.             END;
  1129.     END ELSE BEGIN
  1130.         area := window^.visRgn^^.rgnBBox;
  1131.         GoThroughShapes(AndDrawThem, window);
  1132.     END;
  1133. END; {DrawAllShapes}
  1134.  
  1135.  
  1136. {$S Main}
  1137. PROCEDURE DrawWindow(window: WindowPtr);
  1138.  
  1139. {The core application window updating routine. Understands about Offscreen
  1140.  setup, (in this case, two nested offscreen buffers), and what needs to
  1141.  be drawn, in this case, a whole bunch of shapes. Called from two routines,
  1142.  DoUpdate and DoContentClick. The way it works is first, by calling
  1143.  BeginUpdateOffscreen on fEditHandle, the drawing is redirected to
  1144.  the 'edit' offscreen pixmap. Next, if any drawing needs to be done
  1145.  in the 'background' pixmap, then by calling BeginOffscreenDrawing on
  1146.  fBackHandle, drawing is further redirected. All the shapes that exist
  1147.  but are not the one being edited (i.e., the background) are drawn here
  1148.  and the EndOffscreenDrawing causes the redirecting to cease. Then the
  1149.  pixmap is copybitsed into the next outer layer of drawing, whether that
  1150.  is the 'edit' offscreen pixmap or the window itself. There, the shape
  1151.  being edited is drawn. Finally, EndUpdateOffscreen is called to cease
  1152.  that layer of redirection and copybits the 'edit' offscreen to the window.
  1153.  The way this is designed, it all still works if either or both of the
  1154.  offscreen pixmaps is missing.}
  1155.  
  1156. VAR
  1157.     globalRect    : Rect;
  1158.     drawNeeded    : BOOLEAN;
  1159.     backMap        : BitMapPtr;
  1160.     
  1161. BEGIN
  1162.     GetGlobalRect(window, globalRect);
  1163.     WITH OffscreenPeek(window)^ DO BEGIN
  1164.         IF CheckBoundsOffscreen(fEditHandle, globalRect, drawNeeded) <> noErr THEN {do nada};
  1165.         SetPort(window);
  1166.         BeginUpdateOffscreen(fEditHandle, window);    {this sets up the visRgn}
  1167.         
  1168.         IF CheckBoundsOffscreen(fBackHandle, globalRect, drawNeeded) <> noErr THEN 
  1169.                                                     {do nada};
  1170.         IF drawNeeded THEN BEGIN                    {draw if updating needs to be done}
  1171.             BeginOffscreenDrawing(fBackHandle, window);
  1172.             EraseRect(window^.portRect);            {clear out any garbage that might}
  1173.             DrawAllShapes(window, FALSE);            {be left behind and draw the}
  1174.             EndOffscreenDrawing(fBackHandle);        {'background'}
  1175.         END;
  1176.         backMap := GetMap(fBackHandle);
  1177.         IF backMap <> NIL THEN BEGIN
  1178.             ForeColor(blackColor);
  1179.             BackColor(whiteColor);                    {so funny colorization doesn't happen}
  1180.             WITH window^ DO BEGIN
  1181.                 CopyBits(backMap^, portBits, portRect, portRect, srcCopy, NIL);
  1182.                 ValidRectOffscreen(fBackHandle, NIL, portRect);
  1183.             END;
  1184.         END;
  1185.         DrawAllShapes(window, TRUE);                {only draw the edited shape}
  1186.         
  1187.         EndUpdateOffscreen(fEditHandle, window);
  1188.         CheckTitle(window, TRUE);                    {buffers may have changed}
  1189.     END;
  1190. END; {DrawWindow}
  1191.  
  1192.  
  1193. {$S Main}
  1194. PROCEDURE DoContentClick (window: WindowPtr; event: EventRecord);
  1195.  
  1196. {This is called when a mouse-down event occurs in the content of a window.
  1197.  Other applications might want to call FindControl, TEClick, etc., to
  1198.  further process the click. In Offsample, a user click in the content
  1199.  region means a shape is to be added or changed.}
  1200.  
  1201. VAR
  1202.     oldRect, newRect        : Rect;
  1203.     anchorPt, oldPt, nextPt    : Point;
  1204.     lastShape                : Shapes;
  1205.     first                    : BOOLEAN;
  1206.     
  1207.     PROCEDURE AndReorderThem (shape: Shapes);
  1208.     
  1209.     {Remove the edited shape from the linked list of shapes.}
  1210.     
  1211.     BEGIN
  1212.         WITH OffscreenPeek(window)^ DO
  1213.             IF shape <> gShape THEN
  1214.                 lastShape := shape
  1215.             ELSE
  1216.                 IF fFirst = shape THEN
  1217.                     fFirst := fShapes[shape].next
  1218.                 ELSE
  1219.                     fShapes[lastShape].next := fShapes[shape].next;
  1220.     END; {AndReorderThem}
  1221.  
  1222. BEGIN
  1223.     IF IsAppWindow(window) THEN
  1224.         WITH OffscreenPeek(window)^ DO BEGIN
  1225.             anchorPt := event.where;
  1226.             GlobalToLocal(anchorPt);
  1227.             oldPt := anchorPt;
  1228.             
  1229.             {If the shape being edited existed previously, we need
  1230.              to invalidate its old position so that it gets
  1231.              'erased'.}
  1232.              
  1233.             IF ORD(fShapes[gShape].next) <> kNotDrawn THEN BEGIN
  1234.                 oldRect := GetInvalExtent(window, gShape);
  1235.                 InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1236.                 InvalRectOffscreen(fEditHandle, window, oldRect);
  1237.             END;
  1238.             fEdit := gShape;                            {flag this shape as edited}
  1239.             lastShape := Shapes(kLastOne);
  1240.             GoThroughShapes(AndReorderThem, window);
  1241.             IF ORD(lastShape) <> kLastOne THEN
  1242.                 fShapes[lastShape].next := gShape        {make edited shape last}
  1243.             ELSE
  1244.                 fFirst := gShape;                        {or if only shape, first}
  1245.             fShapes[gShape].next := Shapes(kLastOne);
  1246.             Pt2Rect(anchorPt, anchorPt, oldRect);
  1247.             first := TRUE;                                {indicate first time though loop}
  1248.             WHILE WaitMouseUp DO BEGIN                    {while the mouse is down…}
  1249.                 GetMouse(nextPt);
  1250.                 IF first | (NOT EqualPt(oldPt, nextPt)) THEN BEGIN
  1251.                     first := FALSE;                        {no longer first time through loop}
  1252.                     oldPt := nextPt;
  1253.                     CASE gShape OF
  1254.                         kOval,                            {build a rectangle for these}
  1255.                         kRegion,                        {from the anchor point and}
  1256.                         kRRect,                            {the current point}
  1257.                         kPoly,
  1258.                         kRect:
  1259.                             Pt2Rect(anchorPt, nextPt, newRect);
  1260.                         kICON:                            {rect from current position}
  1261.                             WITH nextPt, newRect DO BEGIN
  1262.                                 top := v;
  1263.                                 left := h;
  1264.                                 bottom := top + 32;
  1265.                                 right := left + 32;
  1266.                             END;
  1267.                         kPICT:                            {rect from current position}
  1268.                             WITH nextPt, newRect DO BEGIN
  1269.                                 newRect := gPICT^^.picFrame;
  1270.                                 OffsetRect(newRect, -left, -top);
  1271.                                 OffsetRect(newRect, h, v);
  1272.                             END;
  1273.                     END;
  1274.                     fShapes[gShape].extent := newRect;
  1275.                     UnionRect(newRect, oldRect, oldRect);
  1276.                     
  1277.                     {In the case of the 'stretchable' shapes whose extents are
  1278.                      built from the anchor point and the current point, doing
  1279.                      a UnionRect is pretty close to being as efficient as doing
  1280.                      a UnionRgn with two regions that are shaped like the old
  1281.                      and new extents. However, a case can be made for using
  1282.                      regions for the icon and the picture since they move around
  1283.                      instead of 'stretching'. The effect of extra, unnecessary
  1284.                      invalidation is, of course, most noticeable when there is
  1285.                      no edit offscreen and the icon/picture is moved around
  1286.                      rapidly. Changing the code to use regions is LEFT AS AN
  1287.                      EXERCISE FOR THE READER, Ha-Ha-Ha.}
  1288.                      
  1289.                     InvalRectOffscreen(fEditHandle, window, oldRect);
  1290.                     DrawWindow(window);
  1291.                     oldRect := GetInvalExtent(window, gShape);
  1292.                 END;
  1293.             END;
  1294.             fEdit := Shapes(kNotDrawn);
  1295.             oldRect := GetInvalExtent(window, gShape);
  1296.             InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1297.         END;
  1298. END; {DoContentClick}
  1299.  
  1300.  
  1301. {$S Main}
  1302. PROCEDURE DoUpdate(window: WindowPtr);
  1303.  
  1304. {This is called when an update event is received for a window.
  1305.  It calls DrawWindow to draw the contents of an application window.}
  1306.  
  1307. BEGIN
  1308.     IF IsAppWindow(window) THEN
  1309.         DrawWindow(window);
  1310. END; {DoUpdate}
  1311.  
  1312.  
  1313. {$S Main}
  1314. PROCEDURE DoActivate(window: WindowPtr; becomingActive: BOOLEAN);
  1315.  
  1316. {This is called when a window is activated or deactivated.}
  1317.  
  1318. BEGIN
  1319.     IF IsAppWindow(window) THEN
  1320.         IF gMac.hasColorQD & becomingActive THEN
  1321.             SetObjCursor(window);
  1322. END; {DoActivate}
  1323.  
  1324.  
  1325. {$S Main}
  1326. PROCEDURE AdjustCursor(region: RgnHandle);
  1327.  
  1328. {Change the cursor's shape, depending on its position. This also calculates the region
  1329.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  1330.  that region, an event is generated, causing this routine to be called. This
  1331.  allows us to change the region to the region the mouse is currently in. If
  1332.  there is more to the event than just “the mouse moved”, we get called before the
  1333.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  1334.  this is called again before we fall back into WNE.}
  1335.  
  1336. VAR
  1337.     window                : WindowPtr;
  1338.     arrowRgn            : RgnHandle;
  1339.     shapeRgn            : RgnHandle;
  1340.     globalPortRect        : Rect;
  1341.     mouse                : Point;
  1342.  
  1343. BEGIN
  1344.     window := FrontWindow;
  1345.     {we only adjust the cursor when we are in front}
  1346.     IF (NOT gInBackground) AND (NOT IsDAWindow(window)) THEN BEGIN
  1347.         GetMouse(mouse);
  1348.         LocalToGlobal(mouse);
  1349.         
  1350.         {calculate regions for different cursor shapes}
  1351.         arrowRgn := NewRgn;
  1352.         shapeRgn := NewRgn;
  1353.  
  1354.         {start with a big, big rectangular region}
  1355.         SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg,
  1356.                             kExtremePos, kExtremePos);
  1357.  
  1358.         {calculate shapeRgn}
  1359.         IF IsAppWindow(window) THEN BEGIN
  1360.             SetPort(window);            {make a global version of the portRect}
  1361.             IF gMac.hasColorQD THEN
  1362.                 WITH CGrafPtr(window)^ DO
  1363.                     SetOrigin(-portPixMap^^.bounds.left, -portPixMap^^.bounds.top)
  1364.             ELSE
  1365.                 WITH window^.portBits.bounds DO
  1366.                     SetOrigin(-left, -top);
  1367.             globalPortRect := window^.portRect;
  1368.             RectRgn(shapeRgn, globalPortRect);
  1369.             SectRgn(shapeRgn, window^.visRgn, shapeRgn);
  1370.             SetOrigin(0, 0);
  1371.         END;
  1372.  
  1373.         {subtract other regions from arrowRgn}
  1374.         DiffRgn(arrowRgn, shapeRgn, arrowRgn);
  1375.  
  1376.         {change the cursor and the region parameter}
  1377.         IF PtInRgn(mouse, shapeRgn) THEN BEGIN
  1378.             IF gMac.hasColorQD THEN
  1379.                 SetCCursor(gCursor)
  1380.             ELSE
  1381.                 SetCursor(GetCursor(crossCursor)^^);
  1382.             CopyRgn(shapeRgn, region);
  1383.         END ELSE BEGIN
  1384.             SetCursor(arrow);
  1385.             CopyRgn(arrowRgn, region);
  1386.         END;
  1387.  
  1388.         {get rid of our local regions}
  1389.         DisposeRgn(arrowRgn);
  1390.         DisposeRgn(shapeRgn);
  1391.     END;
  1392. END; {AdjustCursor}
  1393.  
  1394.  
  1395. {$S Main}
  1396. PROCEDURE DoEvent(event: EventRecord);
  1397.  
  1398. {Do the right thing for an event. Determine what kind of event it is, and call
  1399.  the appropriate routines.}
  1400.  
  1401. VAR
  1402.     part, err    : INTEGER;
  1403.     window        : WindowPtr;
  1404.     ignore        : BOOLEAN;
  1405.     key            : CHAR;
  1406.     aPoint        : Point;
  1407.     fi            : FailInfo;
  1408.     
  1409.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  1410.     BEGIN
  1411.         IF error > 0 THEN
  1412.             AlertUser(0, error)
  1413.         ELSE
  1414.             AlertUser(error, message);
  1415.         EXIT(DoEvent);
  1416.     END; {HandleErr}
  1417.  
  1418. BEGIN
  1419.     CatchFailures(fi, HandleErr);
  1420.     CASE event.what OF
  1421.         mouseDown: BEGIN
  1422.             part := FindWindow(event.where, window);
  1423.             CASE part OF
  1424.                 inMenuBar: BEGIN            {process the menu command}
  1425.                     AdjustMenus;
  1426.                     DoMenuCommand(MenuSelect(event.where));
  1427.                 END;
  1428.                 inSysWindow:                {let the system handle the mouseDown}
  1429.                     SystemClick(event, window);
  1430.                 inContent:
  1431.                     IF window <> FrontWindow THEN BEGIN
  1432.                         SelectWindow(window);
  1433.                         {DoEvent(event);}    {use this line for "do first click"}
  1434.                     END ELSE
  1435.                         DoContentClick(window, event);
  1436.                 inDrag:                        {pass screenBits.bounds to get all gDevices}
  1437.                     DragWindow(window, event.where, screenBits.bounds);
  1438.                 inGrow:;
  1439.                 inZoomIn, inZoomOut:;
  1440.                 inGoAway:
  1441.                     IF TrackGoAway(window, event.where) THEN
  1442.                         ignore := DoCloseWindow(window);
  1443.             END;
  1444.         END;
  1445.         keyDown, autoKey: BEGIN                {check for menukey equivalents}
  1446.             key := CHR(BAnd(event.message, charCodeMask));
  1447.             IF BAnd(event.modifiers, cmdKey) <> 0 THEN    {Command key down}
  1448.                 IF event.what = keyDown THEN BEGIN
  1449.                     AdjustMenus;            {enable/disable/check menu items properly}
  1450.                     DoMenuCommand(MenuKey(key));
  1451.                 END;
  1452.         END;                                {call DoActivate with the window and...}
  1453.         activateEvt:                        {TRUE for activate, FALSE for deactivate}
  1454.             DoActivate(WindowPtr(event.message), BAnd(event.modifiers, activeFlag) <> 0);
  1455.         updateEvt:                          {call DoUpdate with the window to update}
  1456.             DoUpdate(WindowPtr(event.message));
  1457.         diskEvt:
  1458.             IF HiWrd(event.message) <> noErr THEN BEGIN
  1459.                 SetPt(aPoint, kDILeft, kDITop);
  1460.                 err := DIBadMount(aPoint, event.message);
  1461.             END;
  1462.         kOSEvent:
  1463.             CASE BAnd(BRotL(event.message, 8), $FF) OF    {high byte of message}
  1464.                 kSuspendResumeMessage: BEGIN
  1465.                     gInBackground := BAnd(event.message, kResumeMask) = 0;
  1466.                     DoActivate(FrontWindow, NOT gInBackground);
  1467.                 END;
  1468.             END;
  1469.     END;
  1470.     Success(fi);
  1471. END; {DoEvent}
  1472.  
  1473.  
  1474. {$S Main}
  1475. PROCEDURE EventLoop;
  1476.  
  1477. {Get events forever, and handle them by calling DoEvent.
  1478.  Get the events by calling WaitNextEvent, if it's available, otherwise
  1479.  by calling GetNextEvent. Also call AdjustCursor each time through the loop.}
  1480.  
  1481. VAR
  1482.     cursorRgn    : RgnHandle;
  1483.     gotEvent    : BOOLEAN;
  1484.     event        : EventRecord;
  1485.  
  1486. BEGIN
  1487.     cursorRgn := NewRgn;            {we’ll pass WNE an empty region the 1st time thru}
  1488.     REPEAT
  1489.         IF gHasWaitNextEvent THEN {put us 'asleep' forever under MultiFinder}
  1490.             gotEvent := WaitNextEvent(everyEvent, event, MAXLONGINT, cursorRgn)
  1491.         ELSE BEGIN
  1492.             SystemTask;                {must be called if using GetNextEvent}
  1493.             gotEvent := GetNextEvent(everyEvent, event);
  1494.         END;
  1495.         IF gotEvent THEN BEGIN
  1496.             AdjustCursor(cursorRgn); {make sure we have the right cursor}
  1497.             DoEvent(event);
  1498.         END;
  1499.         AdjustCursor(cursorRgn);
  1500.     UNTIL FALSE;                    {loop forever; we quit through an ExitToShell}
  1501. END; {EventLoop}
  1502.  
  1503.  
  1504. PROCEDURE _DataInit; EXTERNAL;
  1505.  
  1506. {This routine is part of the MPW runtime library. This external
  1507.  reference to it is done so that we can unload its segment, %A5Init.}
  1508.  
  1509. {$S Main}
  1510. BEGIN
  1511.     UnloadSeg(@_DataInit);    {note that _DataInit must not be in Main!}
  1512.     
  1513.     MoreMasters;
  1514.     MoreMasters;
  1515.     MoreMasters;            {prepare for handles used by Offscreen}
  1516.     
  1517.     {If you have stack requirements that differ from the default,
  1518.      then you could use SetApplLimit to increase StackSpace at 
  1519.      this point, before calling MaxApplZone.}
  1520.      
  1521.     MaxApplZone;            {expand the heap so code segments load at the top}
  1522.  
  1523.     InitSignals;
  1524.     Initialize;                {initialize the program}
  1525.     UnloadSeg(@Initialize);    {note that Initialize must not be in Main!}
  1526.  
  1527.     EventLoop;                {call the main event loop}
  1528. END.
  1529.